package cn.panda.edu.service.impl;

import cn.panda.edu.dao.EnterMapper;
import cn.panda.edu.dao.StudentMapper;
import cn.panda.edu.model.Enter;
import cn.panda.edu.model.Student;
import cn.panda.edu.service.EnterService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EnterServiceImpl implements EnterService {

	@Autowired
	private EnterMapper enterMapper;
	@Autowired
	private StudentMapper studentMapper;
	
	@Override
	@Transactional
	public int updateByPrimaryKeySelective(Enter record) {
		// TODO Auto-generated method stub
		Enter enter = enterMapper.selectByPrimaryKey(record.getEnterId());
		Student student = new Student();
		student.setMajorId(enter.getMajorId());
		student.setStudentName(enter.getEnterName());
		student.setStudentPassword("000000");
		student.setStudentIdcard(enter.getEnterIdcard());
		student.setStudentSex(enter.getEnterSex());
		student.setStudentNation(enter.getEnterNation());
		student.setStudentAddress(enter.getEnterAddress());
		student.setStudentBirth(enter.getEnterBirth());
		student.setStudentCertificate(enter.getEnterCertificate());
		student.setStudentDate(new Date());
		student.setStudentType(enter.getEnterType().toString());
		student.setStudentEducation(enter.getEnterEducation());
		student.setStudentEmail(enter.getEnterEmail());
		student.setStudentPhone(enter.getEnterPhone());
		student.setStudentPhoto(enter.getEnterPhoto());
		if (!"".equals(enter.getEnterPayMoney()) && enter.getEnterPayMoney() != null) {
			student.setStudentFirstMoney(new BigDecimal(enter.getEnterPayMoney()) );
		}else {
			student.setStudentFirstMoney(null);
		}
		
		student.setStudentFirstMoneyTime(enter.getEnterPayDate());
		student.setStudentGraduate(enter.getEnterGraduate());
		student.setStudentSecondMoney(new BigDecimal("10000"));
		student.setStudentGraduation("未毕业");
		student.setStudentModifyDate(new Date());
		student.setStudentLastTime(new Date());
		student.setStudentDelete(1);
		studentMapper.insertSelective(student);
		return enterMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public PageInfo<Enter> selectByTranStatus(int pageIndex, int pageSize,String enterName) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<>();
		map.put("enterName", enterName);
		PageHelper.startPage(pageIndex,pageSize);
		List<Enter> eList = enterMapper.selectByTranStatus(map);
		PageInfo<Enter> pageInfo = new PageInfo<>(eList);
		return pageInfo;
	}

}
